package com.example.administrator.hqq.adapter.information;

import android.annotation.SuppressLint;
import android.content.Context;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.bitmap.CenterCrop;
import com.example.administrator.hqq.R;
import com.example.administrator.hqq.View.comment.CommentBean;
import com.example.administrator.hqq.View.comment.CommentTextView;
import com.example.administrator.hqq.View.comment.UserBean;
import com.example.administrator.hqq.bean.information.ForumNews;
import com.example.administrator.hqq.utils.TimeUtils;
import com.example.administrator.hqq.utils.transform.GlideCircleTransform;
import com.zhy.autolayout.AutoLinearLayout;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * @name: HQQ
 * @describe:
 * @author: 康海涛 QQ2541849981
 * @time: 2018/12/7 15:18
 * @remark:
 */
public class MessageListAdapter extends BaseAdapter {
    private List<ForumNews.DataBean> data;
    private Context context;
    private ForumDetails forumDetails;

    public void setForumDetails(ForumDetails forumDetails) {
        this.forumDetails = forumDetails;
    }

    public MessageListAdapter(Context context, List<ForumNews.DataBean> data) {
        this.data = data;
        this.context = context;
    }

    @Override
    public int getCount() {
        return data.size();
    }

    @Override
    public Object getItem(int i) {
        return i;
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @SuppressLint("ViewHolder")
    @Override
    public View getView(final int i, View view, ViewGroup viewGroup) {
        ViewHolder viewHolder = null;
        if (view==null)
        {
            view = LayoutInflater.from(context).inflate(R.layout.message_list_item, viewGroup, false);
            viewHolder = new ViewHolder(view);
            view.setTag(viewHolder);
        }
        else {
            viewHolder= (ViewHolder) view.getTag();
        }
        viewHolder.replyContext.setVisibility(View.GONE);
        viewHolder.userName.setText(data.get(i).getUser_nickname());
        Glide.with(context).load(data.get(i).getUser_url()).transform(new CenterCrop(context),new GlideCircleTransform(context)).into(viewHolder.userPic);
        viewHolder.commentTime.setText(TimeUtils.getTimeTypeString(data.get(i).getTime(),"MM-dd HH:mm"));
        viewHolder.forumTitle.setText(data.get(i).getForum_title());
        viewHolder.moduleTitle.setText(data.get(i).getForum_topic_name());
        CommentBean commentBean = new CommentBean();
        commentBean.setComment(data.get(i).getText());
        UserBean replyUser = new UserBean();
        replyUser.setUserName("@"+data.get(i).getTarget_user_nickname());
        commentBean.setReplyUser(replyUser);
        viewHolder.commentContext.setText(commentBean);
        if (TextUtils.isEmpty(data.get(i).getForum_url()))
        {
            viewHolder.forumPic.setVisibility(View.GONE);
        }
        else{
            viewHolder.forumPic.setVisibility(View.VISIBLE);
            Glide.with(context).load(data.get(i).getForum_url()).into(viewHolder.forumPic);
        }
        if (data.get(i).getType().equals("DISCUSSION"))
        {
            viewHolder.replyContext.setVisibility(View.VISIBLE);
            CommentBean commentBean1 = new CommentBean();
            commentBean1.setComment(data.get(i).getTarget_text());
            UserBean commentUser = new UserBean();
            commentUser.setUserName("@"+data.get(i).getTarget_user_nickname());
            commentBean1.setUser(commentUser);
            viewHolder.replyContext.setText(commentBean1);
        }
        view.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (forumDetails!=null){
                    forumDetails.clickMessage(i);
                }
            }
        });
        return view;
    }

    static class ViewHolder {
        @BindView(R.id.user_pic)
        ImageView userPic;
        @BindView(R.id.user_name)
        TextView userName;
        @BindView(R.id.comment_time)
        TextView commentTime;
        @BindView(R.id.title_auto)
        AutoLinearLayout titleAuto;
        @BindView(R.id.comment_context)
        CommentTextView commentContext;
        @BindView(R.id.reply_context)
        CommentTextView replyContext;
        @BindView(R.id.forum_pic)
        ImageView forumPic;
        @BindView(R.id.forum_title)
        TextView forumTitle;
        @BindView(R.id.module_title)
        TextView moduleTitle;
        @BindView(R.id.reply)
        TextView reply;

        ViewHolder(View view) {
            ButterKnife.bind(this, view);
        }
    }
    public interface ForumDetails{
        void clickMessage(int position);
    }
}
